/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI.EditPanes.Filter;

import GUI.Components.BTextField;
import GUI.Components.ColorButton;
import GUI.Components.Float3Panel;
import GUI.Components.BSlider;
import GUI.EditPanes.EditTaskPane;
import General.CurrentData;
import com.jme3.math.Vector2f;
import com.jme3.math.Vector3f;
import com.jme3.water.WaterFilter;
import other.Wizard;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.concurrent.Callable;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import se.datadosen.component.RiverLayout;

/**
 *
 * @author David
 */
public class WaterTaskPane extends EditTaskPane
{

    private WaterFilter waterFilter;
    private BTextField heightField = new BTextField("Float");
    private BTextField speedField = new BTextField("Float");
    private BSlider waveScaleSlider = new BSlider(Float.class, -30, 0, 5);
    private BSlider maxAmplitudeSlider = new BSlider(Float.class, 0, 30, 1);
    private BSlider resolutionSlider = new BSlider(Integer.class, 1, 1024, 512);
    private BSlider transparencySlider = new BSlider(Float.class, -1, 0, .1f);
    private ColorButton waterColorButton = new ColorButton(Color.white);
    private ColorButton deepWaterColorButton = new ColorButton(Color.white);
    private BTextField normalScaleField = new BTextField("Float");
    private BSlider windXSlider = new BSlider(Float.class, -1, 1, 0);
    private BSlider windZSlider = new BSlider(Float.class, -1, 1, 0);
    private BTextField sunScaleField = new BTextField("Float");
    private ColorButton sunColorButton = new ColorButton(Color.white);
    private Float3Panel sunDirectionField = new Float3Panel(Vector3f.NAN);
    private BTextField reflectionDisplaceField = new BTextField("Float");
    private BTextField refractionStrengthField = new BTextField("Float");
    private BTextField foamHardnessField = new BTextField("Float");
    private BTextField foamIntensityField = new BTextField("Float");
    private BTextField causistsIntensityField = new BTextField("Float");
    private BTextField underWaterFogDistanceField = new BTextField("Float");
    private BTextField shoreHardnessField = new BTextField("Float");
    private BTextField shininessField = new BTextField("Float");
    private Float3Panel foamExistencePanel = new Float3Panel(Vector3f.NAN);
    private Float3Panel colorExtinctionPanel = new Float3Panel(Vector3f.NAN);
    private JCheckBox useCausistsCheckBox, useFoamCheckBox, useHQShoreLineCheckBox, useRefractionCheckBox, useRipplesCheckBox, useSpecularCheckBox;

    /**
     *
     * @param wf
     */
    public WaterTaskPane(final WaterFilter wf)
    {
        waterFilter = wf;
        /*Werte zuweisen*/
        speedField.setText("" + waterFilter.getSpeed());
        foamHardnessField.setText("" + waterFilter.getFoamHardness());
        foamIntensityField.setText("" + waterFilter.getFoamIntensity());
        reflectionDisplaceField.setText("" + waterFilter.getReflectionDisplace());
        refractionStrengthField.setText("" + waterFilter.getRefractionStrength());
        sunScaleField.setText("" + waterFilter.getSunScale());
        underWaterFogDistanceField.setText("" + waterFilter.getUnderWaterFogDistance());
        normalScaleField.setText("" + waterFilter.getNormalScale());
        causistsIntensityField.setText("" + waterFilter.getCausticsIntensity());
        shininessField.setText("" + waterFilter.getShininess());
        shoreHardnessField.setText("" + waterFilter.getShoreHardness());
        resolutionSlider.setValue(waterFilter.getReflectionMapSize());
        resolutionSlider.addChangeListener(new ChangeListener()
        {
            public void stateChanged(ChangeEvent e)
            {
                waterFilter.setReflectionMapSize(resolutionSlider.getValue());
            }
        });
        waveScaleSlider._setValue(-waterFilter.getWaveScale() * 1000);
        waveScaleSlider.addChangeListener(new ChangeListener()
        {
            public void stateChanged(ChangeEvent e)
            {
                waterFilter.setWaveScale(-waveScaleSlider._getValue() / 1000);
            }
        });
        maxAmplitudeSlider._setValue(waterFilter.getMaxAmplitude());
        maxAmplitudeSlider.addChangeListener(new ChangeListener()
        {
            public void stateChanged(ChangeEvent e)
            {
                waterFilter.setMaxAmplitude(maxAmplitudeSlider._getValue());
            }
        });
        transparencySlider._setValue(-waterFilter.getWaterTransparency());
        transparencySlider.addChangeListener(new ChangeListener()
        {
            public void stateChanged(ChangeEvent e)
            {
                waterFilter.setWaterTransparency(-transparencySlider._getValue());
            }
        });

        windXSlider._setValue(waterFilter.getWindDirection().getX());
        windZSlider._setValue(waterFilter.getWindDirection().getY());
        windXSlider.addChangeListener(new ChangeListener()
        {
            public void stateChanged(ChangeEvent e)
            {
                waterFilter.setWindDirection(new Vector2f(windXSlider._getValue(), windZSlider._getValue()));
            }
        });
        windZSlider.addChangeListener(new ChangeListener()
        {
            public void stateChanged(ChangeEvent e)
            {
                waterFilter.setWindDirection(new Vector2f(windXSlider._getValue(), windZSlider._getValue()));
            }
        });

        colorExtinctionPanel.setVector(waterFilter.getColorExtinction());
        foamExistencePanel.setVector(waterFilter.getFoamExistence());
        sunDirectionField.setVector(waterFilter.getLightDirection());

        waterColorButton.setColor(Wizard.makeColor(waterFilter.getWaterColor()));
        deepWaterColorButton.setColor(Wizard.makeColor(waterFilter.getDeepWaterColor()));
        sunColorButton.setColor(Wizard.makeColor(waterFilter.getLightColor()));

        useCausistsCheckBox = new JCheckBox("", waterFilter.isUnderWater());
        useFoamCheckBox = new JCheckBox("", waterFilter.isUseFoam());
        useHQShoreLineCheckBox = new JCheckBox("", waterFilter.isUseHQShoreline());
        useRefractionCheckBox = new JCheckBox("", waterFilter.isUseRefraction());
        useRipplesCheckBox = new JCheckBox("", waterFilter.isUseRipples());
        useSpecularCheckBox = new JCheckBox("", waterFilter.isUseSpecular());

        initCheckBoxActions();

        taskPane.setLayout(new RiverLayout(5, 5));
        taskPane.setTitle("Water Filter");
        add(taskPane, BorderLayout.CENTER);
        applyButton.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                waterFilter.setCausticsIntensity(Float.parseFloat(causistsIntensityField.getText()));
                waterFilter.setColorExtinction(colorExtinctionPanel.getVector());
                waterFilter.setDeepWaterColor(Wizard.makeColorRGBA(deepWaterColorButton.getColor()));
                waterFilter.setFoamExistence(foamExistencePanel.getVector());
                waterFilter.setFoamHardness(Float.parseFloat(foamHardnessField.getText()));
                waterFilter.setFoamIntensity(Float.parseFloat(foamIntensityField.getText()));
                waterFilter.setLightColor(Wizard.makeColorRGBA(sunColorButton.getColor()));
                waterFilter.setLightDirection(sunDirectionField.getVector());
                waterFilter.setMaxAmplitude(maxAmplitudeSlider._getValue());
                waterFilter.setNormalScale(Float.parseFloat(normalScaleField.getText()));
                //waterFilter.setReflectionDisplace(Float.parseFloat(reflectionDisplaceField.getText()));
                waterFilter.setReflectionMapSize(resolutionSlider.getValue());
                waterFilter.setRefractionStrength(Float.parseFloat(refractionStrengthField.getText()));
                waterFilter.setShininess(Float.parseFloat(shininessField.getText()));
                waterFilter.setShoreHardness(Float.parseFloat(shoreHardnessField.getText()));
                waterFilter.setSpeed(Float.parseFloat(speedField.getText()));
                waterFilter.setSunScale(Float.parseFloat(sunScaleField.getText()));
                waterFilter.setUnderWaterFogDistance(Float.parseFloat(underWaterFogDistanceField.getText()));
                waterFilter.setUseCaustics(useCausistsCheckBox.isSelected());
                waterFilter.setUseFoam(useFoamCheckBox.isSelected());
                waterFilter.setUseHQShoreline(useHQShoreLineCheckBox.isSelected());
                waterFilter.setUseRefraction(useRefractionCheckBox.isSelected());
                waterFilter.setUseRipples(useRipplesCheckBox.isSelected());
                waterFilter.setUseSpecular(useSpecularCheckBox.isSelected());
                waterFilter.setWaterColor(Wizard.makeColorRGBA(waterColorButton.getColor()));
                waterFilter.setWaterHeight(Float.parseFloat(heightField.getText()));
                waterFilter.setWaterTransparency(transparencySlider._getValue());
                waterFilter.setWaveScale(-waveScaleSlider._getValue() / 1000);
                waterFilter.setWindDirection(new Vector2f(windXSlider._getValue(), windZSlider._getValue()));
            }
        });
        taskPane.add("left", new JLabel("Height:"));
        taskPane.add("tab hfill", heightField);
        taskPane.add("br left", new JLabel("Speed:"));
        taskPane.add("tab hfill", speedField);
        taskPane.add("br left", new JLabel("Wave Scale:"));
        taskPane.add("tab hfill", waveScaleSlider);
        taskPane.add("br left", new JLabel("Max. Amplitude:"));
        taskPane.add("tab hfill", maxAmplitudeSlider);
        taskPane.add("br left", new JLabel("Resolution:"));
        taskPane.add("tab hfill", resolutionSlider);
        taskPane.add("br left", new JLabel("Transparency:"));
        taskPane.add("tab hfill", transparencySlider);
        taskPane.add("br left", new JLabel("Water Color:"));
        taskPane.add("tab hfill", waterColorButton);
        taskPane.add("br left", new JLabel("Deep-Water Color:"));
        taskPane.add("tab hfill", deepWaterColorButton);
        taskPane.add("br left", new JLabel("Normal Scale:"));
        taskPane.add("tab hfill", normalScaleField);
        taskPane.add("br left", new JLabel("Sun Scale:"));
        taskPane.add("tab hfill", sunScaleField);
        taskPane.add("br left", new JLabel("Sun Color:"));
        taskPane.add("tab hfill", sunColorButton);
        taskPane.add("br left", new JLabel("Sun Direction:"));
        taskPane.add("tab hfill", sunDirectionField);
        taskPane.add("br left", new JLabel("Reflection Displace:"));
        taskPane.add("tab hfill", reflectionDisplaceField);
        taskPane.add("br left", new JLabel("Refraction Strength:"));
        taskPane.add("tab hfill", refractionStrengthField);
        taskPane.add("br left", new JLabel("Foam Hardness:"));
        taskPane.add("tab hfill", foamHardnessField);
        taskPane.add("br left", new JLabel("Foam Intensity:"));
        taskPane.add("tab hfill", foamIntensityField);
        taskPane.add("br left", new JLabel("Causists Intensity:"));
        taskPane.add("tab hfill", causistsIntensityField);
        taskPane.add("br left", new JLabel("Underwater Fog Distance:"));
        taskPane.add("tab hfill", underWaterFogDistanceField);
        taskPane.add("br left", new JLabel("Shore Hardness:"));
        taskPane.add("tab hfill", shoreHardnessField);
        taskPane.add("br left", new JLabel("Shininess:"));
        taskPane.add("tab hfill", shininessField);
        taskPane.add("br left", new JLabel("Wind Direction:"));
        taskPane.add("br left", new JLabel("X:"));
        taskPane.add("tab hfill", windXSlider);
        taskPane.add("br left", new JLabel("Z:"));
        taskPane.add("tab hfill", windZSlider);
        taskPane.add("br left", new JLabel("Foam Existence:"));
        taskPane.add("tab hfill", foamExistencePanel);
        taskPane.add("br left", new JLabel("Color Extinction:"));
        taskPane.add("tab hfill", colorExtinctionPanel);
        taskPane.add("br left", new JLabel("Use Specular:"));
        taskPane.add("tab hfill", useSpecularCheckBox);
        taskPane.add("br left", new JLabel("Use Refraction:"));
        taskPane.add("tab hfill", useRefractionCheckBox);
        taskPane.add("br left", new JLabel("Use Foam:"));
        taskPane.add("tab hfill", useFoamCheckBox);
        taskPane.add("br left", new JLabel("Use Causists:"));
        taskPane.add("tab hfill", useCausistsCheckBox);
        taskPane.add("br left", new JLabel("HQ Shoreline:"));
        taskPane.add("tab hfill", useHQShoreLineCheckBox);
        taskPane.add("br left", new JLabel("Use Ripples:"));
        taskPane.add("tab hfill", useRipplesCheckBox);
        taskPane.add("br right", applyButton);
    }

    @Override
    public void updateData()
    {
        if (!heightField.hasFocus())
        {
            heightField.setText("" + waterFilter.getWaterHeight());
        }
    }

    private void initCheckBoxActions()
    {
        useCausistsCheckBox.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                CurrentData.getEditorWindow().getB3DApplication().enqueue(new Callable<Void>()
                {
                    public Void call() throws Exception
                    {
                        waterFilter.setUseCaustics(useCausistsCheckBox.isSelected());
                        return null;
                    }
                });
            }
        });
        useFoamCheckBox.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                CurrentData.getEditorWindow().getB3DApplication().enqueue(new Callable<Void>()
                {
                    public Void call() throws Exception
                    {
                        waterFilter.setUseFoam(useFoamCheckBox.isSelected());
                        return null;
                    }
                });
            }
        });
        useHQShoreLineCheckBox.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                CurrentData.getEditorWindow().getB3DApplication().enqueue(new Callable<Void>()
                {
                    public Void call() throws Exception
                    {
                        waterFilter.setUseHQShoreline(useHQShoreLineCheckBox.isSelected());
                        return null;
                    }
                });
            }
        });
        useRefractionCheckBox.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                CurrentData.getEditorWindow().getB3DApplication().enqueue(new Callable<Void>()
                {
                    public Void call() throws Exception
                    {
                        waterFilter.setUseRefraction(useRefractionCheckBox.isSelected());
                        return null;
                    }
                });
            }
        });
        useRipplesCheckBox.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                CurrentData.getEditorWindow().getB3DApplication().enqueue(new Callable<Void>()
                {
                    public Void call() throws Exception
                    {
                        waterFilter.setUseRipples(useRipplesCheckBox.isSelected());
                        return null;
                    }
                });
            }
        });
        useSpecularCheckBox.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                CurrentData.getEditorWindow().getB3DApplication().enqueue(new Callable<Void>()
                {
                    public Void call() throws Exception
                    {
                        waterFilter.setUseSpecular(useSpecularCheckBox.isSelected());
                        return null;
                    }
                });
            }
        });
    }
}
